package UI.Windows
{
	import Events.*;
	
	import UI.Buttons.UIButton;
	import UI.LessonPlanViewer.*;
	import UI.List.*;
	import UI.Loader.*;
	
	import flash.display.*;
	import flash.events.MouseEvent;
	
	import spark.components.Window;
	
	public class UIHistoryViewWindow extends Window
	{
		private var historyList:UIList;
		private var openButton:UIButton;
		private var deleteButton:UIButton;
		
		public function UIHistoryViewWindow(listItems:Vector.<String>, classAssets:Object)
		{
			super();
			
			openButton = new UIButton(classAssets.OpenButton);
			deleteButton = new UIButton(classAssets.DeleteButton);
			
			historyList=new UIList();
			for each(var s:String in listItems)
			{
				historyList.addItem(new UIListItem(s));
			}
			addEventListeners();
			positionElements();
			addElements();
		}
		
		public override function open(openWindowActive:Boolean=true):void
		{
			super.open(openWindowActive);
			this.width = historyList.width+(nativeWindow.width-nativeWindow.stage.stageWidth);
			this.height = historyList.height+historyList.y+(nativeWindow.height-nativeWindow.stage.stageHeight);
		}
		
		private function openItemDispatch(e:MouseEvent):void
		{
			dispatchEvent(new UIHistoryViewEvent(UIHistoryViewEvent.OPEN,historyList.selection,e.bubbles,e.cancelable));
		}
		
		private function deleteItemDispatch(e:MouseEvent):void
		{
			dispatchEvent(new UIHistoryViewEvent(UIHistoryViewEvent.DELETE,historyList.selection,e.bubbles,e.cancelable));
		}
		
		public function refresh():void
		{
			historyList.removeSelectedItem();
		}
		
		private function addEventListeners():void
		{
			openButton.addEventListener(MouseEvent.CLICK,openItemDispatch);
			deleteButton.addEventListener(MouseEvent.CLICK,deleteItemDispatch);
		}
		
		private function positionElements():void
		{
			deleteButton.x=openButton.width+5;
			historyList.y=Math.max(deleteButton.height,openButton.height)+5;
			historyList.width = deleteButton.x+deleteButton.width;
		}
		
		private function addElements():void
		{
			addElement(historyList);
			addElement(openButton);
			addElement(deleteButton);
		}
	}
}